package com.lucher.mes.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lucher.mes.common.Tools;
import com.lucher.mes.dao.ProducescheduleMapper;
import com.lucher.mes.dao.ProductMapper;
import com.lucher.mes.po.Produceschedule;
import com.lucher.mes.po.Product;

@Controller
@RequestMapping("/produceschedule")  
public class ProducescheduleController extends BaseController{
	@Autowired
	private ProducescheduleMapper producescheduleDao;
	@Autowired
	private ProductMapper productDao;
	
	/**
	 * 查询信息
	 */
	@RequestMapping(value = "/findByList")
	public String findByList(Produceschedule form) {
		return "produceschedule/findProducescheduleList";
	}
	
	/**
	 * 查询信息Json
	 */
	@ResponseBody
	@RequestMapping(value = "/findListJson")
	public Map<String, Object> findListJson(Produceschedule form) {
		//封装JSon的Map
		Map<String, Object> result = new HashMap<String, Object>(); 
		result.put("code", "0");
		List<HashMap<String, Object>> objectlist = new ArrayList<HashMap<String, Object>>();
		
		Integer total = producescheduleDao.findByCount(form);
		List<Produceschedule> list = producescheduleDao.findByListForPage(form);
		
		for (Produceschedule produceschedule : list) {
			HashMap<String, Object> objectMap = new HashMap<String, Object>();
			
			objectMap.put("id", produceschedule.getId());
			objectMap.put("producecode", produceschedule.getProducecode());
			objectMap.put("productcode", produceschedule.getProductcode());
			objectMap.put("productname", produceschedule.getProductname());
			objectMap.put("batchno", produceschedule.getBatchno());
			objectMap.put("linecode", produceschedule.getLinecode());
			objectMap.put("linename", produceschedule.getLinename());
			objectMap.put("sectioncode", produceschedule.getSectioncode());
			objectMap.put("sectionname", produceschedule.getSectionname());
			objectMap.put("machinecode", produceschedule.getMachinecode());
			objectMap.put("prisonercode", produceschedule.getPrisonercode());
			objectMap.put("prisonername", produceschedule.getPrisonername());
			objectMap.put("addtime", StringUtils.isEmpty(produceschedule.getAddtime())?"":Tools.getStr(produceschedule.getAddtime()).substring(0, 19));
			objectMap.put("checkercode", produceschedule.getCheckercode());
			objectMap.put("checkername", produceschedule.getCheckername());
			objectMap.put("remark", produceschedule.getRemark());
			
			objectlist.add(objectMap);
		}
		
		result.put("count", total);
		result.put("data", objectlist);
		return result;
	}

	/**
	 * 添加信息初始化
	 * 
	 * @return
	 */
	@RequestMapping(value = "/addInit")
	public String addInit(Produceschedule form) {
		return "produceschedule/addProduceschedule";
	}

	/**
	 * 新增
	 */
	@ResponseBody //此标志就是返回jesion数据给页面的标志
	@RequestMapping(value = "/save")
	public Map<String,Object>  save(Produceschedule form) {
		
		//封装返回给页面的json对象
		HashMap<String,Object> responseJson = new HashMap<String,Object>();
		responseJson.put("code", "0");
		
		if ("".equals(form.getProducecode())) {
			responseJson.put("flag", "0"); //操作失败
			responseJson.put("msg", "操作失败，请输入产品编码");
			return responseJson;
		} 
		
		//保存信息
		Integer producescheduleId = producescheduleDao.insertSelective(form);
		
		responseJson.put("flag", "1");//操作成功
		responseJson.put("msg", "操作成功");
		
		return responseJson;
	}

	/**
	 * 更新初始化
	 */
	@RequestMapping(value = "/updateInit")
	public String updateInit(Produceschedule form) {
		Produceschedule produceschedule = producescheduleDao.selectByPrimaryKey(form.getId());
		form.setProduceschedule(produceschedule);

		return "produceschedule/updateProduceschedule";
	}

	/**
	 * 更新
	 */
	@ResponseBody //此标志就是返回jesion数据给页面的标志
	@RequestMapping(value = "/update")
	public Map<String,Object> update(Produceschedule form) {
		//封装返回给页面的json对象
		HashMap<String,Object> responseJson = new HashMap<String,Object>();
		responseJson.put("code", "0");
		
		if ("".equals(form.getProductcode())) {
			responseJson.put("flag", "0"); //添加失败
			responseJson.put("msg", "操作失败，产品编码不能为空");
			return responseJson;
		}
		
		// 修改信息
		int flag = producescheduleDao.updateByPrimaryKeySelective(form);
		
		responseJson.put("flag", "1");//
		responseJson.put("msg", "操作成功");
		return responseJson;
	}
    
	/**
	 * 删除
	 * @return
	 */
	@ResponseBody //此标志就是返回jesion数据给页面的标志
	@RequestMapping(value="/delete")
	public Map<String,Object> delete(Produceschedule form) {
		//封装返回给页面的json对象
		HashMap<String,Object> responseJson = new HashMap<String,Object>();
		responseJson.put("code", "0");
		//删除材料
		producescheduleDao.deleteByPrimaryKey(form.getId());
		
		responseJson.put("flag", "1");//删除成功
		responseJson.put("msg", "操作成功");
		
		return responseJson;
	}
	
	/**
	 * 批量删除
	 * @return
	 */
	@ResponseBody //此标志就是返回jesion数据给页面的标志
	@RequestMapping(value="/deleteBatch")
	public Map<String,Object> deleteBatch(@RequestBody List<Produceschedule> formlist) {
		//封装返回给页面的json对象
		HashMap<String,Object> responseJson = new HashMap<String,Object>();
		responseJson.put("code", "0");
		for (Produceschedule produceschedule : formlist) {
			//删除材料
			producescheduleDao.deleteByPrimaryKey(produceschedule.getId());
		}

		responseJson.put("flag", "1");//删除成功
		responseJson.put("msg", "操作成功");
		
		return responseJson;
	}
	
}
